
package com.lt.test.provider.dao.impl;

import com.lt.test.common.util.StringUtils;
import com.lt.test.provider.dao.BaseDaoSupport;
import com.lt.test.provider.dao.KtSupplierDao;
import com.lt.test.provider.mapper.SupplierMapper;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Repository
public class KtSupplierDaoImpl extends BaseDaoSupport implements KtSupplierDao {

	private static final Logger log = Logger.getLogger(KtSupplierDaoImpl.class);

	/**
	 * 根据查询条件获取实现集
	 */
	public List query(Map parameter) {
		List list = new ArrayList();
		try {
			list = this.queryForList("queryKtSuppliers",parameter);
		} catch (Exception e) {
			log.error("错误:");
			e.printStackTrace();
		}
	    return list;
	}


	/**
	 * 创建实体
	 */
    public int create(Map parameter)
    {
    	return this.insert("createKtSupplier", parameter);
    }

    /**
     * 更新实体
     */
    public int update(Map parameter)
    {
    	return this.update("updateKtSupplier",parameter);
    }

    /**
     * 保存实体
     */
    public int save(Map parameter)
    {
    	return this.update("saveKtSupplier",parameter);
    }


     /**
     * 删除实体
     */
    public int delete(java.lang.String id)
    {
    	return this.delete("deleteKtSupplier",id);
    }
    /**
	 * 根据主键获取实体信息
	 */
	public Map findById(java.lang.String id) {
		return this.queryForSingle("queryKtSupplier", id);
	}

  	/**
     * 批量插入
     */
	public int batch(List values){
  		int iEffectedRow = 0;
  		iEffectedRow = this.insert("batchKtSupplier",values);
  		return iEffectedRow;
	}

  	/**
     * 批量更新
     */
  	public int batchUpdate(List values){
  		int iEffectedRow = 0;
  		iEffectedRow = this.update("batchUpdateKtSupplier",values);
  		return iEffectedRow;
	}

  	/**
     * 批量删除
     */
  	public int batchDelete(List values){
  		int iEffectedRow = 0;
  		iEffectedRow = this.delete("batchDeleteKtSupplier",values);
  		return iEffectedRow;
	}


	@Override
	public Map<String, Object> getSupplierListSize(String supplierName, String helpCode) {
		// TODO Auto-generated method stub
		StringBuilder sqlStr = new StringBuilder();
		LinkedList<Object> params = new LinkedList<Object>();
		sqlStr.append("SELECT count(1) total from KT_SUPPLIER t1 WHERE 1=1 ");
		if(!StringUtils.isNullOrEmpty(supplierName)){
			sqlStr.append(" AND T1.SUPPLIER_NAME like ? ");
			params.add("%" + supplierName + "%");
		}
		if(!StringUtils.isNullOrEmpty(helpCode)){
			sqlStr.append(" AND T1.HELP_CODE like ? ");
			params.add("%" + helpCode + "%");
		}
		return this.query(sqlStr.toString(), params.toArray());
	}


	@Override
	public List<Map<String, Object>> getSupplier(String supplierName, String helpCode, Long beginNum,
                                                           Long endNum) {
		// TODO Auto-generated method stub
		StringBuilder sqlStr = new StringBuilder();
		LinkedList<Object> params = new LinkedList<Object>();
		sqlStr.append("SELECT * FROM (SELECT t.*,ROWNUM rn FROM ( ");
		sqlStr.append(" SELECT T1.*, T2.TEXT OPERATOR_WAY_NAME,t3.TEXT SETTLEMENT_WAY_NAME,t4.TEXT DEFAULT_PURCHASE_WAY_NAME,t5.TEXT REGISTER_WAY_NAME ");
		sqlStr.append(" FROM KT_SUPPLIER t1 ");
		sqlStr.append(" LEFT JOIN KT_DICT t2 ON t1.OPERATOR_WAY = T2.CODE AND T2.STATE = '1' AND T2.IDX_CODE='SUPPLIER_OPERATOR_WAY' ");
		sqlStr.append(" LEFT JOIN KT_DICT t3 ON T1.SETTLEMENT_WAY = T3.CODE  AND t3.STATE = '1' AND t3.IDX_CODE='SUPPLIER_SETTLEMENT_WAY' ");
		sqlStr.append(" LEFT JOIN KT_DICT t4 ON T1.DEFAULT_PURCHASE_WAY = T4.CODE  AND t4.STATE = '1' AND t4.IDX_CODE='SUPPLIER_DEFAULT_PURCHASE_WAY' ");
		sqlStr.append(" LEFT JOIN KT_DICT t5 ON T1.REGISTER_WAY = T5.CODE  AND t5.STATE = '1' AND t5.IDX_CODE='SUPPLIER_REGISTER_WAY' ");
		sqlStr.append(" WHERE 1 = 1");
		if(!StringUtils.isNullOrEmpty(supplierName)){
			sqlStr.append(" AND T1.SUPPLIER_NAME like ? ");
			params.add("%" + supplierName + "%");
		}
		if(!StringUtils.isNullOrEmpty(helpCode)){
			sqlStr.append(" AND T1.HELP_CODE like ? ");
			params.add("%" + helpCode + "%");
		}
		sqlStr.append(" ORDER BY T1.CREATE_TIME,T1.MODIFY_TIME DESC)t WHERE  ? >=ROWNUM )t WHERE   t.rn >=? ");
		params.add(endNum);
		params.add(beginNum);

		return this.queryAsArray(sqlStr.toString(), params.toArray());
	}
}
